package common

import (
	"github.com/spf13/viper"
)
import _ "github.com/spf13/viper/remote"

type Setting struct {
	vp *viper.Viper
}

func NewLocalSetting() (*Setting, error) {
	vp := viper.New()
	vp.SetConfigName("config")
	vp.SetConfigType("yaml")
	vp.AddConfigPath("config/")
	err := vp.ReadInConfig()
	if err != nil {
		return nil, err
	}
	return &Setting{vp}, nil
}

func NewConsulSetting(url string, fileKey string) (*Setting, error) {
	vp := viper.New()
	var err error
	err = vp.AddRemoteProvider("consul", url, fileKey)
	if err != nil {
		return nil, err
	}
	vp.SetConfigType("yaml")
	err = vp.ReadRemoteConfig()
	if err != nil {
		return nil, err
	}
	return &Setting{vp}, nil
}
